
import * as type from './type'
import axios from 'axios'

const state={
    todaydata:null,
    tomorrowdata:null
}

export const actionCreators={
    get_today_data(){
        return dispatch=>{ 
             //请求数据
             axios({
                url:'/mock/data.json'
            })
            .then(data=>{
               const action={
                   type:type.GET_TODAY_DATA,
                   payload:data.data
               }
               console.log(data)
               dispatch(action)
            })
            .catch(error=>console.log("error:",error))
        }
    },
    get_tomorrow_data(){
        return dispatch=>{ 
             //请求数据
             axios({
                url:'/mock/data_tomorrow.json'
            })
            .then(data=>{
                const action={
                    type:type.GET_TOMORROW_DATA,
                    payload:data.data
                }
                dispatch(action)
                console.log("data",data)
            })
             .catch(error=>console.log("error:",error))

        }
    }
}

export const reducer=(previousState=state,action)=>{
    const newState={
        ...previousState
    }
    switch(action.type){
        case type.GET_TODAY_DATA:
            newState.todaydata=action.payload
        break;
        case type.GET_TOMORROW_DATA:
            newState.tomorrowdata=action.payload
        break;
        default:
        break;
    }
    return newState
}